package com.warm.base.jdk.grammar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2019/10/31 16:45
 * @Author: LChuang
 * @Description:
 */
public class CollectionsTest {
    private static final Integer MAX_NUMBER = 3;

    public static void main(String[] args) {

        /*List<Integer> source = Arrays.asList(1,2,3,4, 1,2,3, 1,2, 1,2,3, 2,5,6,8);
        //List<Map<String, List<Integer>>> l = result(source);
        //System.out.println(l);
        List<List<Integer>> list = new ArrayList<>();
        List<List<Integer>> result = subList(source, list);
        System.out.println(result);*/

        // 集合差集
        /*List<String> a = new ArrayList<String>();
        a.add("a");
        a.add("b");
        a.add("c");
        List<String> b = new ArrayList<String>();
        b.add("a");
        b.add("b");
        b.add("d");

        System.out.println(a.equals(b));
        System.out.println(a.containsAll(b));

        System.out.println(b.removeAll(a));

        System.out.println(b);*/

        /*List<SysUser> list = new ArrayList<>();

        list.add(new SysUser("黄一",11));
        list.add(new SysUser("黄一",22));
        list.add(new SysUser("黄三",33));
        list.add(new SysUser("黄三",33));

        List<String> dupl = list.stream()
                .collect(Collectors.groupingBy(user -> user.getName(), Collectors.counting()))
                .entrySet().stream()
                .filter(entry -> entry.getValue() > 1)
                .map(entry -> entry.getKey())
                .collect(Collectors.toList());

        System.out.println(dupl.isEmpty());
        System.out.println(dupl);*/
    }

    /**
     * 集合拆分
     * @param list
     * @param result
     * @return
     */
    private static List<List<Integer>> subList(List<Integer> list, List<List<Integer>> result){
        boolean isSplit = false;
        int index = 0;
        //判断是否需要拆分
        for (int i=0; i < list.size(); i++){
            if(i+1 < list.size() && list.get(i) > list.get(i+1)){
                index = i;
                isSplit = true;
                break;
            }
        }

        if(!isSplit){
            //不用拆分
            result.add(list);
        }else {
            //需要拆分
            List<Integer> sub = list.subList(0, index+1);
            List<Integer> end = list.subList(index+1, list.size());
            result.add(sub);
            subList(end, result);
        }
        return result;
    }


    private static Boolean check(List<Integer> list){
        boolean flag = true;
        for (int i=0; i < list.size(); i++){
            if(i+1 < list.size() && list.get(i) > list.get(i+1)){
                flag = false;
                break;
            }
        }
        return flag;
    }

    private static List<Integer> get(List<Integer> source){
        List<Integer> indexs = new ArrayList<>();
        for (int i=0; i < source.size(); i++){
            if(i+1 < source.size() && source.get(i) > source.get(i+1)){
                indexs.add(i);
            }
        }
        return indexs;
    }

    private static List<Map<String, List<Integer>>> result(List<Integer> source){
        List<Integer> indexs = get(source);
        List<Map<String, List<Integer>>> resultMap = new ArrayList<>();
        String a = "S02";
        if(!indexs.isEmpty()){
            int start = 0;
            for(int i = 0; i < indexs.size(); i++){
                Map<String, List<Integer>> e = new HashMap<>();
                // subList() --> 包左不包右
                e.put(a, source.subList(start, indexs.get(i)+1));
                resultMap.add(e);
                start = indexs.get(i)+1;
            }
            Map<String, List<Integer>> e = new HashMap<>();
            e.put(a, source.subList(indexs.get(indexs.size()-1)+1, source.size()));
            resultMap.add(e);

        }else {
            Map<String, List<Integer>> e = new HashMap<>();
            e.put(a, source);
            resultMap.add(e);
        }
        return resultMap;
    }
}
